from knowledge_base_directory import knowledge_base_directory
from knowledge_base_directory import phrase_file_extension
from knowledge_base_directory import phrase_file_creation_time
from knowledge_base_directory import phrase_file_modification_time
from knowledge_base_directory import phrase_file_size

class mediator_fake :
    def __init__ ( self ) :
        self . _created_files = [ ]
        self . _deleted_files = [ ]
        self . _read_files = { }
        self . _call_history = [ ]
        self . _current_time_request = False
    def current_time_request ( self ) :
        self . _current_time_request = True
    def create_file ( self , name , contents ) :
        self . _call_history += [ mediator_fake . create_file ]
        self . _created_files += [ [ name , contents ] ]
    def delete_file ( self , name ) :
        self . _call_history += [ mediator_fake . delete_file ]
        self . _deleted_files += [ name ]
    def read_file_request ( self , name ) :
        self . _call_history += [ mediator_fake . read_file_request ]
        self . _directory . read_file_reply ( name , self . _read_files [ name ] )

class knowledge_base_directory_scheduled_tests :
    'knowledge_base_directory_scheduled'
    def __init__ ( self , test ) :
        self . _test = test
        self . _mediator = mediator_fake ( )
        self . _directory = knowledge_base_directory ( self . _mediator )
        self . _mediator . _directory = self . _directory
    def test_create_knowledge_base ( self ) :
        self . _directory . create_knowledge_base ( 'my base' )
        self . _test . assume_true ( self . _mediator . _current_time_request )
        self . _directory . current_time_reply ( 'my time' )
        self . _test . assume_equal ( self . _mediator . _created_files , [ [ 'my base' + phrase_file_extension ( ) , 
            [ phrase_file_creation_time ( ) + 'my time' 
            , phrase_file_modification_time ( ) + 'my time'
            , phrase_file_size ( ) + '0'
            ] ] ] )
        self . _directory . current_time_reply ( 'my new time' )
        self . _test . assume_equal_len ( self . _mediator . _created_files , 1 )
    def test_rename_knowledge_base ( self ) :
        self . _mediator . _read_files = { 'my base' + phrase_file_extension ( ) : 
            [ phrase_file_creation_time ( ) + 'my creation time'
            , phrase_file_modification_time ( ) + 'my modification time'
            , phrase_file_size ( ) + 'my size'
            ] }
        self . _directory . rename_knowledge_base ( 'my base' , 'my new base' )
        self . _test . assume_true ( self . _mediator . _current_time_request )
        self . _directory . current_time_reply ( 'my current time' )
        self . _test . assume_equal ( self . _mediator . _created_files , [ [ 'my new base' + phrase_file_extension ( ) , 
            [ phrase_file_creation_time ( ) + 'my creation time' 
            , phrase_file_modification_time ( ) + 'my current time'
            , phrase_file_size ( ) + 'my size'
            ] ] ] )
        self . _test . assume_equal ( self . _mediator . _deleted_files , [ 'my base' + phrase_file_extension ( ) ] )
        self . _test . assume_equal ( self . _mediator . _call_history [ - 1 ] , mediator_fake . delete_file )
        self . _directory . current_time_reply ( 'my new time' )
        self . _test . assume_equal_len ( self . _mediator . _deleted_files , 1 )
    def test_copy_knowledge_base ( self ) :
        self . _mediator . _read_files = { 'my base' + phrase_file_extension ( ) : 
            [ phrase_file_creation_time ( ) + 'my creation time'
            , phrase_file_modification_time ( ) + 'my modification time'
            , phrase_file_size ( ) + 'my size'
            ] }
        self . _directory . copy_knowledge_base ( 'my base' , 'my new base' )
        self . _test . assume_true ( self . _mediator . _current_time_request )
        self . _directory . current_time_reply ( 'my current time' )
        self . _test . assume_equal ( self . _mediator . _created_files , [ [ 'my new base' + phrase_file_extension ( ) , 
            [ phrase_file_creation_time ( ) + 'my creation time' 
            , phrase_file_modification_time ( ) + 'my current time'
            , phrase_file_size ( ) + 'my size'
            ] ] ] )
        self . _directory . current_time_reply ( 'my new time' )
        self . _test . assume_equal_len ( self . _mediator . _created_files , 1 )
